import Vue from 'vue'
import VueRouter from 'vue-router'

//导入一级路由

import Login from '@/components/Login'
import Home from '@/components/Home'
//导入二级路由
// import Users from '@/views/users'
// import Roles from '@/views/roles'
// import Rights from '@/views/rights'
// import Reports from '@/views/reports'
// import Params from '@/views/params'
// import Orders from '@/views/orders'
// import Goods from '@/views/goods'
// import Categories from '@/views/categories'
// import GoodsAdd from '@/views/goodsAdd'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // 初始进入到login页面
    redirect: 'login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [
      // { path: '/users', component: Users },
      { path: '/users', component: () => import('../views/users.vue') },
      // { path: '/roles', component: Roles },
      { path: '/roles', component: () => import('../views/roles.vue') },
      // { path: '/rights', component: Rights },
      { path: '/rights', component: () => import('../views/rights.vue') },
      // { path: '/reports', component: Reports },
      { path: '/reports', component: () => import('../views/reports.vue') },
      // { path: '/params', component: Params },
      { path: '/params', component: () => import('../views/params.vue') },
      // { path: '/orders', component: Orders },
      { path: '/orders', component: () => import('../views/orders.vue') },
      // { path: '/goods', component: Goods },
      { path: '/goods', component: () => import('../views/goods.vue') },
      // { path: '/goodsadd', component: GoodsAdd },
      { path: '/goodsadd', component: () => import('../views/goodsAdd.vue') },
      // { path: '/categories', component: Categories }
      { path: '/categories', component: () => import('../views/categories.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach(function(to, from, next) {
  // router.beforeEach((to, from, next) => {
  //判断token  如果有 你已经登陆过  那就放行
  //如果没有token   说明没有登录    跳转登录页面
  //如果输入login直接放行
  if (to.path == '/login') {
    return next()
  }
  var token = window.sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})

//  解决路由版本的兼容性的
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
